// 成本子级占比排名
var numberFormate = iot.getNumberFormat("0.00");
/*var subLevelDevMap = {
    719 : {
        ele : 697,
        gas : 567
    }
};*/
dev.action = function(resp,param){
    var spaces = param.workSpaceName.split('/');
    var factoryName = spaces[spaces.length-1]
    //是否算总还是单
    var totalOrSingle = param.selectType;
    if (totalOrSingle == null){
        totalOrSingle = "total";
    }
    var theTime = param.time;
    var workSpace = param.workSpace;
    // var factoryId = '';
    var datas = iot.list();
    var legend = iot.list();
    var db = iot.db();
    // var resultFactory = db.qry("select es.id,es.factory,es.name from energy_struc es where es.pid = " + workSpace);
    // var factory = iot.list();
    // resultFactory.forAll(function(item,ix,cnt){
    //     factory.add(item.name);
    //     factoryId = factoryId + item.id + ","
    // });
    // factoryId = factoryId + 0;
    // var timeSub = parseInt(theTime.substring(0,4));

    var resultChildRank;
    // var electricityResult = null;
    // var gasResult = null;
    // if(timeSub >= 2019) {
    resultChildRank = db.qry("select es.name factory, es.factory as f, SUM(win.dvalue) totalSum \n" +
        "from iot_dev_data_win win\n" +
        "LEFT JOIN energy_struc es on es.pid = "+ workSpace +"\n" +
        "LEFT JOIN energy_struc_prop esp on esp.energy_struc_id = es.id\n" +
        "where 1=1\n" +
        "and win.dev_id = esp.dev_id\n" +
        "and win.data_type = esp.name\n" +
        "and win.the_time like '"+ theTime +"%'\n" +
        "GROUP BY win.dev_id;");
    // }else{
    //
    //     subLevelDevMap = getFactoryParam(factoryName, "成本")
    //     if(subLevelDevMap[workSpace] && subLevelDevMap[workSpace].ele) {
    //         electricityResult = db.qry("select esp.energy_struc_id, es.name factory, SUM(dev_win.dvalue) totalSum " +
    //             " from iot_dev_data_win dev_win," +
    //             " (select * from energy_struc_prop e where type = 1 and name = '_用量' and energy_struc_id in (select es.id from energy_struc es where es.pid = "+ subLevelDevMap[workSpace].ele +" and es.name != '其他')) esp" +
    //             " LEFT JOIN energy_struc es" +
    //             " on esp.energy_struc_id = es.id" +
    //             " where dev_win.dev_id = esp.dev_id" +
    //             " and dev_win.data_type = esp.name" +
    //             " and dev_win.the_time like '" + theTime + "%'" +
    //             " GROUP BY energy_struc_id");
    //     }
    //     if(subLevelDevMap[workSpace] && subLevelDevMap[workSpace].gas) {
    //         gasResult = db.qry("select esp.energy_struc_id, es.name factory, SUM(dev_win.dvalue) totalSum " +
    //             " from iot_dev_data_win dev_win," +
    //             " (select * from energy_struc_prop e where type = 1" +
    //             " -- and name = '用量'\n" +
    //             " and energy_struc_id in (select es.id from energy_struc es where es.pid = "+ subLevelDevMap[workSpace].gas +" and es.name != '其他')) esp" +
    //             " LEFT JOIN energy_struc es" +
    //             " on esp.energy_struc_id = es.id" +
    //             " where dev_win.dev_id = esp.dev_id" +
    //             " and dev_win.data_type = esp.name" +
    //             " and dev_win.the_time like '" + theTime + "%'" +
    //             " GROUP BY energy_struc_id");
    //     }
    // }
    //获取产量
    // var yieldId = getFactoryYieldId(factoryName)

    // var yieldResult = getFactoryYield(yieldId, theTime)

    // var totalYield = yieldResult.totalSum;
    resultChildRank.forAll(function (item) {
        var sum = 0;
        sum = item.totalSum;
        /*if(timeSub >= 2019) {
            resultChildRank.forAll(function (item, ix, cnt) {
                if (item && item.factory && item.factory === factory[index]) {
                    sum = item.totalSum;
                }
            })
        }
        else{
            //获取工厂名称
            var factoryName = getFactoryName(param.workSpaceName);
            if (electricityResult != null) {
                electricityResult.forAll(function (item) {
                    if (item && item.factory && item.factory === factory[index]) {
                        sum = item.totalSum * (carbonRowTimeModulus[factoryName] ? carbonRowTimeModulus[factoryName][timeSub].ele : 0);
                    }
                });
            }
            if (gasResult != null) {
                gasResult.forAll(function (item) {
                    if (item && item.factory && item.factory === factory[index]) {
                        sum = sum + item.totalSum * (carbonRowTimeModulus[factoryName] ? carbonRowTimeModulus[factoryName][timeSub].gas : 0);
                    }
                });
            }
        }*/

        //判断当前选择总数还是单车
        // if (totalOrSingle.equals("total")) {
        datas.add({name: item.factory, value: numberFormate.format(sum)});
        legend.add(item.factory);
        /*}else if (totalOrSingle.equals("single")) {
            if (sum && totalYield){
                sum = sum/totalYield;
            }
            datas.add({name: factory[index], value: numberFormate.format(totalYield === null ? 0 : sum)});
            legend.add(factory[index]);
        }*/
    })

    var jsData = {
        datas : datas,
        legend: legend
    };
    resp.setJsData(jsData);
}